﻿using System;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Document;
using DBAccessLibrary.Model;
using System.Windows.Media.Imaging;

namespace SQLQuery.Data
{
    /// <summary>
    /// 下拉框 实体
    /// </summary>
    public class FieldCompletionData : ICompletionData
    {
        public FieldCompletionData(DataBaseEntity entity)
        {
            this.DBEntity = entity;
        }

        public System.Windows.Media.ImageSource Image
        {
            get { return new BitmapImage(new Uri("../" + DBEntity.ImageUrl, UriKind.Relative)); }
        }

        public DataBaseEntity DBEntity { get; private set; }
        public string Text { get { return DBEntity.TransName; } }
        public string ChText
        {
            get
            {
                FieldEntity entity = DBEntity as FieldEntity;
                if (entity != null)
                {
                    return string.Format("{0}({1}[{2}]{3})", entity.TransChName, entity.TypeName, entity.Length, entity.IsNull);
                }
                return DBEntity.TransChName;
            }
        }

        public object Content
        {
            get { return this.Text; }
        }

        public object Description
        {
            get { return this.ChText == string.Empty ? this.Text : this.ChText; }
        }

        public double Priority { get { return 0; } }

        public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
        {
            textArea.Document.Replace(completionSegment, this.Text);
        }
    }
}
